#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<malloc.h>
#include<string.h>

int max(int a, int b) {
	return a > b ? a : b;
}

int main() {
	int T;
	int n;
	int work[3000][2];
	scanf("%d %d", &T, &n);
	for (int i = 0; i < n; i++) {
		scanf("%d %d", &work[i][0], &work[i][1]);
	}

	int* dp = malloc(sizeof(int) * (T + 1));
	memset(dp, 0, sizeof(int) * (T + 1));

	for (int i = 0; i < n; i++) {
		for (int j = T; j >= work[i][0]; j--) {
			dp[j] = max(dp[j], dp[j - work[i][0]] + work[i][1]);
		}
	}

	printf("%d", dp[T]);
	return 0;

}